$(function () {
	//修改单元
	$("#btn_unitUpdate").on("click", function(){
		var unitId = $("input[name='unitId']").val();
		var parentName = $("input[name='parentName']").val();
		unitUpdate(unitId, parentName);
	});
	
	//批量修改,修改内容为房源
	$("#btn_unitBatchUpdate").on("click", function(){
		var projectId=$("#projectId").val();
		var unitId = $("input[name='unitId']").val();
		var url=jypath+"/estate/projectRoom/toBatchUpdate?projectId="+projectId+"&unitId="+unitId;
		window.location.href=url;
	});
	
	//删除单元
	$("#btn_unitDel").on("click", function(){
		var unitId = $("input[name='unitId']").val();
		del(unitId);
	});
	
	
});

//楼栋修改弹出层
function unitUpdate(unitId, parentName){
	JY.Ajax.doRequest(null,jypath+'/estate/projectUnit/view',{id:unitId},function(data){
		setUnitForm(data);
		$("#unitForm label[name='parentName']").text(parentName);
    	save("修改楼栋","/estate/projectUnit/edit", unitId);
	});
}
function save(title, url, id){
	JY.Model.edit("unitDiv",title,function(){
		if(JY.Validate.form("unitForm")){
			var that =$(this);
			JY.Ajax.doRequest("unitForm",jypath +url,null,function(data){
				that.dialog("close");
	        	JY.Model.info(data.resMsg,function(){
	        		var url = jypath + "/estate/projectUnit/detail?id="+id;
	        		$.post(url, null, function(data){
	        			$("#rightContentDiv").html(data);
	        		});
        		});    
			 });
		}	
	});
}

function del(id){
	JY.Model.confirm("该操作将同步删除其下所有子孙节点的信息!&emsp;是否确认删除？",function(){	
		JY.Ajax.doRequest(null,jypath+'/estate/projectUnit/delete',{id:id},function(data){
			JY.Model.info(data.resMsg,function(){
				$("#rightContentDiv").html("");
 				if(data.res == 1){
 	 				deleteNode();
 	 			}
			});
		});
	});
}

function setUnitForm(data){
	var obj = data.obj;
	$("#unitForm input[name$='id']").val(JY.Object.notEmpty(obj.id));
	$("#unitForm input[name$='unitCode']").val(JY.Object.notEmpty(obj.unitCode));
	$("#unitForm input[name$='unitName']").val(JY.Object.notEmpty(obj.unitName));
	$("#unitForm textarea[name$='remark']").val(JY.Object.notEmpty(obj.remark));
}

